-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[SROA] Prevent load atomic vector from being generated #112432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
These are illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter.
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-llvm-transforms Author: None (jofrn) ChangesThese are illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter. Full diff: https://github.com/llvm/llvm-project/pull/112432.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 92589ab17da313..450ecdf20ef009 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -2853,6 +2853,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
bool visitLoadInst(LoadInst &LI) {
LLVM_DEBUG(dbgs() << " original: " << LI << "\n");
+
+ // load atomic vector would be generated, which is illegal
+ if (LI.isAtomic() && NewAI.getAllocatedType()->isVectorTy())
+ return false;
+
Value *OldOp = LI.getOperand(0);
assert(OldOp == OldPtr);
diff --git a/llvm/test/Transforms/SROA/atomic-vector.ll b/llvm/test/Transforms/SROA/atomic-vector.ll
new file mode 100644
index 00000000000000..d43ae653fba1dd
--- /dev/null
+++ b/llvm/test/Transforms/SROA/atomic-vector.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -passes='sroa' -S 2>&1 | FileCheck %s --check-prefix=ERR
+; RUN: opt < %s -passes='sroa' -S | FileCheck %s
+
+define float @atomic_vector() {
+; ERR-NOT: atomic load operand must have integer, pointer, or floating point type!
+; ERR-NOT: <1 x float> {{%.*}} = load atomic volatile <1 x float>, ptr {{%.*}} acquire, align 4
+; CHECK: %1 = alloca <1 x float>, align 4
+; CHECK-NEXT: store <1 x float> undef, ptr %1, align 4
+; CHECK-NEXT: %2 = load atomic volatile float, ptr %1 acquire, align 4
+; CHECK-NEXT: ret float %2
+ %1 = alloca <1 x float>
+ %2 = alloca <1 x float>
+ %3 = alloca ptr
+ call void @llvm.memcpy.p0.p0.i64(ptr %2, ptr %1, i64 4, i1 false)
+ store ptr %2, ptr %3
+ %4 = load ptr, ptr %3
+ %5 = load atomic volatile float, ptr %4 acquire, align 4
+ ret float %5
+}
|
This is a follow-up to #111414, which used -disable-verify. This commit prevents atomic load vector at all. |
@@ -2853,6 +2853,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> { | |||
|
|||
bool visitLoadInst(LoadInst &LI) { | |||
LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); | |||
|
|||
// load atomic vector would be generated, which is illegal | |||
if (LI.isAtomic() && NewAI.getAllocatedType()->isVectorTy()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it illegal from LangRef's perspective?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is at least illegal from the perspective of IR/Verifier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is written here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The restriction is dumb and we should relax it. Instead of just hardcoding isVectorTy here, should have some kind of LoadInst::isValidAtomicType or something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The verifier check should be moved into a LoadInst helper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No alloca reference, just the type. The implementation should match Verifier::visitLoadInst, the isIntOrIntPtrTy || isFloatingPointTy and checkAtomicMemAccessSize (also add some non-atomic size tests?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to ensure an atomic does not have a vector type. If the load is atomic and the alloca that will lend its type over has a vector type, then we will generate an atomic vector, which are illegal. We need to ensure that this doesn't occur.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you need to ensure the atomic is a valid type for an atomic load.
Alternatively you can do the load with the equivalent sized type and then bitcast (which is why this restriction is dumb in the first place, the lowering can always do the same)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are ensuring it is a valid type in the case under question, by checking the alloca's type. The invalid load will not be generated if the alloca has a vector type as that type will overwrite the load's type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want any loads that are atomic to have a vector type, regardless of whether the atomic itself is already valid; so checking if the atomic is more valid before translating it in AllocaSliceRewriter will miss cases where we form an invalid atomic during SROA (and then later form a vector type with it in visitLoadInst). Even though it is not likely as SROA probably won't form these, it illustrates why we don't need the extra checks here.
; CHECK-NEXT: [[TMP2:%.*]] = load atomic volatile float, ptr [[TMP1]] acquire, align 4 | ||
; CHECK-NEXT: ret float [[TMP2]] | ||
; | ||
%1 = alloca <1 x float> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use named values in tests
; CHECK-NEXT: ret float [[TMP2]] | ||
; | ||
%1 = alloca <1 x float> | ||
%2 = alloca <1 x float> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test an integer and pointer vector too
; CHECK-NEXT: ret i32 [[A_0_LOAD_EXT]] | ||
; CHECK-NEXT: [[A:%.*]] = alloca i1, align 1 | ||
; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[A]] seq_cst, align 4 | ||
; CHECK-NEXT: ret i32 [[V]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to this test, we may want to check only the type without DL
. Adding too many checks hinders optimization.
llvm/lib/IR/Instructions.cpp
Outdated
if (AO == AtomicOrdering::Release || AO == AtomicOrdering::AcquireRelease) | ||
return false; | ||
unsigned Size = DL.getTypeSizeInBits(Ty); | ||
return Size >= 8 && !(Size & (Size - 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
basictest.ll
fails due to checking DL
here.
llvm/lib/IR/Instructions.cpp
Outdated
if (!Ty->isIntOrPtrTy() && !Ty->isFloatingPointTy()) | ||
return false; | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct return of boolean expression
llvm/lib/Transforms/Scalar/SROA.cpp
Outdated
@@ -2853,6 +2853,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> { | |||
|
|||
bool visitLoadInst(LoadInst &LI) { | |||
LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); | |||
|
|||
// load atomic vector would be generated, which is illegal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// load atomic vector would be generated, which is illegal | |
// Load atomic vector would be generated, which is illegal. |
%indirect = load ptr, ptr %direct | ||
%ret = load atomic volatile ptr, ptr %indirect acquire, align 4 | ||
ret ptr %ret | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test a <2 x i16> or some other real vector. 1 x is a degenerate case
llvm/include/llvm/IR/Instructions.h
Outdated
@@ -252,7 +252,8 @@ class LoadInst : public UnaryInstruction { | |||
|
|||
/// Returns false if this type would be invalid in the | |||
/// creation of a load atomic instruction. | |||
static bool isValidAtomicTy(Type *Ty); | |||
static bool isValidAtomicTy(Type *Ty, const DataLayout *DL = nullptr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataLayout must be mandatory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we need to omit it entirely because otherwise we will be checking the type size of the alloca, which we want to avoid if to optimize basictest.ll
.
llvm/lib/IR/Instructions.cpp
Outdated
if (!Ty->isIntOrPtrTy() && !Ty->isFloatingPointTy()) | ||
return false; | ||
if (AO == AtomicOrdering::Release || AO == AtomicOrdering::AcquireRelease) | ||
return false; | ||
if (DL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mandatory
%indirect = load ptr, ptr %direct | ||
%ret = load atomic volatile i32, ptr %indirect acquire, align 4 | ||
ret i32 %ret | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test for the non-byte illegal case?
198a549
to
db0a8fe
Compare
load atomic <n x T>
is illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter.